/**
 * @(#) NoticeProxy.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 공지사항 쓰기 - Proxy
 * 				공지사항 조회하는 Model Logic 
 */
package com.skt.common_hr.alnotice.model
{
	import com.skt.common_hr.alnotice.AlnoticeFacade;
	import com.skt.common_hr.alnotice.model.vo.NoticeVO;
	import com.skt.common_hr.alnotice.model.vo.UseDateVO;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.AlertHelper;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class NoticeWriteProxy extends CommonProxy
	{
		public static const NAME:String = "NoticeWriteProxy";
		
		public var writeKey:String;


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function NoticeWriteProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function loadNoticeKey():void
		{
			var params:Object = new Object();
			params["I_NR_NAME"]  = "ZHRBOARD";
			params["I_BOARD_ID"] = "01";
			loadData("Z_HR_0054",params, loadKeyResult, loadKeyFault);
		}		
		

		public function insert(record:NoticeVO, ac:ArrayCollection):void
		{
			var tables:Object 		= new Object();
			var table:Array 		= new Array();
			
			var params:Object 		= new Object();
			params["I_ACTIO"]		= "OK";
			params["I_PERNR"]		= Variables.myPernr;
			params["I_BEGDA"]		= record.BEGDA_JSON;
			params["I_ENDDA"]		= record.ENDDA_JSON;
			
											
 			var itab:Object 		= new Object();
 			
 			var sltd_udVo:UseDateVO;
 			for(var i:int=0; i<ac.length; i++) {
				sltd_udVo = ac.getItemAt(i) as UseDateVO;
				if(sltd_udVo.isVisible == true) {
					itab["UBEG0"+(i+1)] = DateUtil.toBasicString(sltd_udVo.UBEG_DATE);
					itab["UEND0"+(i+1)] = DateUtil.toBasicString(sltd_udVo.UEND_DATE);
				} 				
 			}

//                              retObj[field+'_DATE'] = DateUtil.stringToDateTime(obj[field]);
//                              retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DATE']);
//                              retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DATE']);
		
			//itab = record;
			table.push(itab);
			tables["ITAB"] = table;
			
			
			saveData("Z_HR_AN02", params, tables, insertResult, insertFault);
			
			//loadData("Z_HR_LE02", params, insertResult, insertFault);
		}


		public function update(record:NoticeVO, sInsertType:String):void
		{			
			var tables:Object 			= new Object();
			var table:Array = new Array();
			
			var params:Object 		= new Object();
			params["I_ACTIO"]		= "MOD";
			params["I_PERNR"]		= Variables.myPernr;
//			params["I_HENUM"]		= record.HENUM;
			
											
			var itab:Object 	= new Object();		
			itab = record;
			table.push(itab);
			tables["ITAB"] = table;
			
			saveData("Z_HR_HE02", params, tables, updateResult, updateFault);
		}
		
		
		public function reply_insert(oReply:Object, sInsertType:String):void
		{
			var tables:Object 			= new Object();
			var table:Array = new Array();
			
			var params:Object 		= new Object();
			params["I_ACTIO"]		= "MOD";//sInsertType;
			params["I_PERNR"]		= Variables.myPernr;
			params["I_HLNUM"]		= oReply.HLNUM;
			
			var itab:Object 				= new Object();		
			itab["RCONTENT"]			= oReply.RCONTENT;		
			itab["RCONTX"]			= oReply.RCONTX;
			table.push(itab);
			tables["ITAB"] = table;
			
			
			saveData("Z_HR_LE02", params, tables, insertResult, insertFault);
			
			//loadData("Z_HR_LE02", params, insertResult, insertFault);
		}

		// 선택검진
		public function loadReserve_Medcd(MEDCK:String, STCD2:String, setFunc:Function):void
		{
			var params:Object = new Object();
			//params["I_PERNR"] 		= Variables.myPernr;
			params["I_MEDCK"] 		= MEDCK;
			params["I_STCD2"]		= STCD2;
			
			loadData("Z_HR_HE04", params, loadReserve_MedcdResult, listFault, setFunc); 
		}
		public function loadReserve_MedcdResult(event:ResultEvent):void
		{
			if(retCode==0) {
				//mainList = parseMainList(event.result.O_ITAB);
				event.result.func(event.result)
				trace("1");
			}
			else {
				Alert.show('loadListResult err: ' + retText);
			}
		}
		
		// 추가검진
		public function loadReserve(BEGDA:Date, STCD2:String, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_PERNR"] 		= Variables.myPernr;
			params["I_MREDAT"] 	= DateUtil.toBasicString(BEGDA);
			params["I_STCD2"]		= STCD2;
			
			loadData("Z_HR_HE03", params, loadReserveResult, listFault, setFunc); 
		}
		public function loadReserveResult(event:ResultEvent):void
		{
			if(retCode==0) {
				//mainList = parseMainList(event.result.O_ITAB);
				event.result.func(event.result)
				trace("1");
			}
			else {
				Alert.show('loadListResult err: ' + retText);
			}
		}


		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}		


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function loadKeyResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				writeKey = event.result["O_CONTENT_IDX"];
				facade.sendNotification(AlnoticeFacade.LOAD_KEY_COMPLETE, this);
			}
			else {
				facade.sendNotification(AlnoticeFacade.LOAD_KEY_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function loadKeyFault(event:FaultEvent):void
		{
			facade.sendNotification(AlnoticeFacade.LOAD_KEY_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		

		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function insertResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				facade.sendNotification(AlnoticeFacade.INSERT_NOTICE_COMPLETE, this);
			}
			else {
				trace( "failed to insert article: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				AlertHelper.showMessage(event.result["RETTEXT"], "오류");
				facade.sendNotification(AlnoticeFacade.INSERT_NOTICE_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function insertFault(event:FaultEvent):void
		{
			trace("Fault message in CertificateProxy: " + event.fault.faultString);
			facade.sendNotification(AlnoticeFacade.INSERT_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function updateResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				facade.sendNotification(AlnoticeFacade.UPDATE_NOTICE_COMPLETE, this);
			}
			else {
				trace(event.result["RETCODE"]+" : "+event.result["RETTEXT"]);
				AlertHelper.showMessage(event.result["RETTEXT"], "오류");
				facade.sendNotification(AlnoticeFacade.INSERT_NOTICE_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function updateFault(event:FaultEvent):void
		{
			facade.sendNotification(AlnoticeFacade.UPDATE_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		public function fileInfoResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				//facade.sendNotification(NoticeFacade.FILEINFO_UPDATE_NOTICE_COMPLETE, this);
			}
			else {
				trace(event.result["RETCODE"]+" : "+event.result["RETTEXT"]);
				//facade.sendNotification(NoticeFacade.FILEINFO_UPDATE_NOTICE_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fileInfoFault(event:FaultEvent):void
		{
			//facade.sendNotification(NoticeFacade.FILEINFO_UPDATE_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}





		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
	}
}